#!/usr/bin/perl -w

# This is a simple fvwm module written in perl using the supplied perl library.
# Iconifies new windows, auto-raises windows and terminates itself in 1 minute.
# Either place this module in FVWM_MODULEDIR or run it using the full path.

use strict;
use lib `fvwm-perllib dir`;
use FVWM::Module;

my $new_window_handler = sub {
	my ($self, $event) = @_;
	#$self->send("Echo M_ADD_WINDOW " . join(", ", %{$event->args}));

	# auto iconify all new windows
	$self->send("Iconify true", $event->_win_id);
};

my $module = new FVWM::Module(
	Name => "FvwmPerlBasedExample",         # used in config and debug
	Mask => M_ADD_WINDOW | M_FOCUS_CHANGE,  # request two these events
);

# do some funny harm
$module->show_message("started");
$module->send("Beep");
$module->send("All (FvwmConsole) Iconify true");

# undo harm
my $on_exit = sub { $_[0]->send("Beep\nAll (FvwmConsole) Iconify false") };

# auto-exit in 60 seconds; a mere die() in ALRM signal handler is ok too
local $SIG{ALRM} = sub { $module->terminate(); };
alarm(60);

# auto-iconify new windows
$module->add_handler(M_ADD_WINDOW, $new_window_handler);
# auto-raise all windows with delay of 500 msec
$module->add_handler(M_FOCUS_CHANGE, sub {
	my $win_id = $_[1]->_win_id;
	$_[0]->send("Deschedule 1234\nSchedule 500 1234 WindowId $win_id Raise");
});
# do a clean-up on exit
$module->add_handler(ON_EXIT, $on_exit);

# enter the main event loop
$module->event_loop();

# this is only reached when the mutual connection with fvwm is lost
$module->show_message("finished");
